package com.sskj.wallet.ui.fragment;

import android.support.constraint.ConstraintLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.util.CommonUtil;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.bus.WalletAssetBean;
import com.sskj.wallet.component.DaggerUserDataComponent;
import com.sskj.wallet.presenter.WalletAssetPresenter;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/06/26
 */
@Route(path = RConfig.WALLET_FRAGMENT_ASSET)
public class WalletAssetFragment extends BaseFragment<WalletAssetPresenter> {
    @BindView(R2.id.constraint)
    ConstraintLayout constraint;
    @BindView(R2.id.tv_wallet_name)
    TextView tvWalletName;
    @BindView(R2.id.iv_see)
    ImageView ivSee;
    @BindView(R2.id.tv_wallet_usdt)
    TextView tvWalletUsdt;
    @BindView(R2.id.tv_wallet_cny)
    TextView tvWalletCny;
    @BindView(R2.id.tv_transfer)
    TextView tvTransfer;
    @BindView(R2.id.tv_income_yesterday)
    TextView tvIncomeYesterday;
    @BindView(R2.id.tv_income_total)
    TextView tvIncomeTotal;
    @BindView(R2.id.tv0)
    TextView tv0;
    @BindView(R2.id.tv1)
    TextView tv1;
    @Autowired
    String walletType;

    private int isSee;
    private String canSee;
    private String cny = "0.00";
    private String usdt = "0.0000";
    private String yesterday_income = "0.00";
    private String total_income = "0.00";

    @Override
    public int getLayoutId() {
        return R.layout.wallet_fragment_wallet_asset;
    }

    @Override
    public WalletAssetPresenter getPresenter() {
        return new WalletAssetPresenter();
    }

    @Override
    public void initView() {
        RxBus.getDefault().register(this);
        ARouter.getInstance().inject(this);
        tvWalletUsdt.setText("0.0000");
        tvWalletCny.setText("≈ 0.00 CNY");
        tvIncomeYesterday.setText("0.00");
        tvIncomeTotal.setText("0.00");
        if (walletType.equals(Constans.WALLET_HUOQI)) {
            canSee = SPUtil.get(SPConfig.HUOQI_EYE, "1");
            tvWalletName.setText(App.INSTANCE.getString(R.string.wallet_walletAssetFragment1));
            tv0.setText(App.INSTANCE.getString(R.string.wallet_walletAssetFragment2));
            tv1.setText(App.INSTANCE.getString(R.string.wallet_walletAssetFragment3));
            constraint.setBackgroundResource(R.mipmap.img_huoqi);
        } else if (walletType.equals(Constans.WALLET_JIEDAI)) {
            canSee = SPUtil.get(SPConfig.JIEDAI_EYE, "1");
            tvWalletName.setText(App.INSTANCE.getString(R.string.wallet_walletAssetFragment4));
            tv0.setText(App.INSTANCE.getString(R.string.wallet_walletAssetFragment2));
            tv1.setText(App.INSTANCE.getString(R.string.wallet_walletAssetFragment3));
            constraint.setBackgroundResource(R.mipmap.img_jiedai);
        } else if (walletType.equals(Constans.WALLET_LIANGHUA)) {
            canSee = SPUtil.get(SPConfig.LIANGHUA_EYE, "1");
            tvWalletName.setText(App.INSTANCE.getString(R.string.wallet_walletAssetFragment5));
            tv0.setText(App.INSTANCE.getString(R.string.wallet_walletAssetFragment6));
            tv1.setText(App.INSTANCE.getString(R.string.wallet_walletAssetFragment3));
            constraint.setBackgroundResource(R.mipmap.img_lianghua);
        }
    }


    @Override
    public void initData() {
        //划转
        ClickUtil.click(300, tvTransfer, () -> {
            if (checkCetification()) {
                ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_TRANSFER)
                        .withString(Constans.WALLET_TYPE, walletType).navigation();
            }
        });
        ClickUtil.click(300, ivSee, () -> {
            if (isSee == 0) {
                if (walletType.equals(Constans.WALLET_HUOQI)) {
                    SPUtil.put(SPConfig.HUOQI_EYE, "0");
                } else if (walletType.equals(Constans.WALLET_JIEDAI)) {
                    SPUtil.put(SPConfig.JIEDAI_EYE, "0");
                } else if (walletType.equals(Constans.WALLET_LIANGHUA)) {
                    SPUtil.put(SPConfig.LIANGHUA_EYE, "0");
                }
                RxBus.getDefault().send(RxBusCode.CLOSE_EYE);
                tvWalletUsdt.setText("******");
                tvWalletCny.setText("******");
                tvIncomeYesterday.setText("******");
                tvIncomeTotal.setText("******");
                ivSee.setImageResource(R.mipmap.lib_unsee);
                isSee = 1;
            } else {
                if (walletType.equals(Constans.WALLET_HUOQI)) {
                    SPUtil.put(SPConfig.HUOQI_EYE, "1");
                } else if (walletType.equals(Constans.WALLET_JIEDAI)) {
                    SPUtil.put(SPConfig.JIEDAI_EYE, "1");
                } else if (walletType.equals(Constans.WALLET_LIANGHUA)) {
                    SPUtil.put(SPConfig.LIANGHUA_EYE, "1");
                }
                RxBus.getDefault().send(RxBusCode.OPEN_EYE);
                tvWalletUsdt.setText(usdt);
                tvWalletCny.setText("≈ " + cny + " CNY");
                tvIncomeYesterday.setText(yesterday_income);
                tvIncomeTotal.setText(total_income);
                ivSee.setImageResource(R.mipmap.lib_see);
                isSee = 0;
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updataAsset(WalletAssetBean walletAssetBean) {
        if (walletAssetBean == null) {
            return;
        }
        if (walletAssetBean.getWalletType().equals(Constans.WALLET_HUOQI) && walletType.equals(Constans.WALLET_JIEDAI)) {
            updataUI(walletAssetBean);
        } else if (walletAssetBean.getWalletType().equals(Constans.WALLET_JIEDAI) && walletType.equals(Constans.WALLET_HUOQI)) {
            updataUI(walletAssetBean);
        } else if (walletAssetBean.getWalletType().equals(walletType)) {
            updataUI(walletAssetBean);
        }
    }

    private void updataUI(WalletAssetBean walletAssetBean) {
        if (walletType.equals(Constans.WALLET_HUOQI)) {
            canSee = SPUtil.get(SPConfig.HUOQI_EYE, "1");
        } else if (walletType.equals(Constans.WALLET_JIEDAI)) {
            canSee = SPUtil.get(SPConfig.JIEDAI_EYE, "1");
        } else if (walletType.equals(Constans.WALLET_LIANGHUA)) {
            canSee = SPUtil.get(SPConfig.LIANGHUA_EYE, "1");
        }
        ivSee.setImageResource("1".equals(canSee) ? R.mipmap.lib_see : R.mipmap.lib_unsee);
        usdt = CommonUtil.dealCoinNum(walletAssetBean.getToto_price(), Constans.COIN_USDT);
        cny = NumberUtil.keep2(walletAssetBean.getToto_cny_pric());
        yesterday_income = NumberUtil.keep2(walletAssetBean.getPrice_tot());
        total_income = NumberUtil.keep2(walletAssetBean.getTotol());
        if ("1".equals(canSee)) {
            tvIncomeYesterday.setText(NumberUtil.keep2(walletAssetBean.getPrice_tot()));
            tvIncomeTotal.setText(NumberUtil.keep2(walletAssetBean.getTotol()));
            tvWalletUsdt.setText(CommonUtil.dealCoinNum(walletAssetBean.getToto_price(), Constans.COIN_USDT));
            tvWalletCny.setText("≈ " + NumberUtil.keep2(walletAssetBean.getToto_cny_pric()) + " CNY");
        } else {
            tvWalletUsdt.setText("******");
            tvWalletCny.setText("******");
            tvIncomeYesterday.setText("******");
            tvIncomeTotal.setText("******");
        }

    }
}
